package com.goldmantis.alitrip.controller.authorization;

import com.goldmantis.alitrip.common.ReturnMessage;
import com.goldmantis.alitrip.common.annotation.Log;
import com.goldmantis.alitrip.common.sysenum.EnumLogCatagory;
import com.goldmantis.alitrip.common.sysenum.EnumOperationType;
import com.goldmantis.alitrip.pojo.SysDataPrivileges;
import com.goldmantis.alitrip.service.SysDataPrivilegesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @Author:Shmily
 * @Description:
 * @Date:Create in 10:39 2018/2/28
 */
@Controller
@RequiresAuthentication
@Api(description = "系统数据权限")
@RequestMapping("sys/data/privilege")
public class SysDataPrivilegeController {

    @Autowired
    private SysDataPrivilegesService sysDataPrivilegesService;

    @PostMapping()
    @ApiOperation(value = "新增数据权限", httpMethod = "POST")
    @Log(operationName = "分配数据权限", logCatagory = EnumLogCatagory.access, operationType = EnumOperationType.add)
    public ResponseEntity<ReturnMessage> post(@ApiParam("数据权限对象") @RequestBody @Valid SysDataPrivileges sysDataPrivileges) {
        sysDataPrivilegesService.insertBatch(sysDataPrivileges);
        return ResponseEntity.ok(ReturnMessage.success("保存成功!"));
    }

    @GetMapping()
    @ApiOperation(value = "根据用户Id，系统Id获取数据权限信息", httpMethod = "GET")
    @Log(operationName = "根据用户Id，系统Id获取数据权限信息", logCatagory = EnumLogCatagory.access, operationType = EnumOperationType.query)
    public ResponseEntity<ReturnMessage> selectBySystemIdAndUserId(
            @ApiParam(value = "系统Id", required = true) @RequestParam Long systemId,
            @ApiParam(value = "用户Id", required = true) @RequestParam Long userId) {
        return ResponseEntity.ok(ReturnMessage.success(sysDataPrivilegesService.selectBySystemIdAndUserId(systemId, userId)));
    }

    @GetMapping("getUserDataPrivilegesByUserId")
    @ApiOperation(value = "根据用户Id获取该用户数据权限详细信息", httpMethod = "GET")
    @Log(operationName = "根据用户Id获取该用户数据权限详细信息", logCatagory = EnumLogCatagory.access, operationType = EnumOperationType.query)
    public ResponseEntity<ReturnMessage> getUserDataPrivilegesByUserId(
            @ApiParam(value = "用户Id", required = true) @RequestParam Long userId,
            @ApiParam(value = " 系统Id") @RequestParam(required = false) Long systemId,
            @ApiParam(value = " 部门名称") @RequestParam(required = false) String orgName,
            @ApiParam("当前页") @RequestParam(defaultValue = "1") Integer offset,
            @ApiParam("每页显示数量") @RequestParam(required = false) Integer limit) {
        return ResponseEntity.ok(ReturnMessage.success(sysDataPrivilegesService.getUserDataPrivilegesByUserId(userId, systemId, orgName, offset, limit)));
    }
}
